package cn.exrick.manager.controller;

import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbNews;
import cn.exrick.manager.pojo.TbUser;
import cn.exrick.manager.service.NewsService;
import cn.exrick.manager.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/news/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取资讯列表")
    public DataTablesResult getNewsList(int draw, int start, int length, TbNews tbNews,
                                         @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","a.title","a.create_time","a.update_time", "a.status", "a.is_top"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "a.update_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }

        DataTablesResult result= newsService.getNewsList(draw,start,length,tbNews,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/news/del/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除资讯")
    public Result<Object> delNews(@PathVariable Long[] ids){
        newsService.deleteNews(Arrays.asList(ids));
        return new ResultUtil<Object>().setData(null);
    }

    private TbUser getCurrentUser(){
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        TbUser user = userService.getUserByUsername(username);
        return user;
    }

    @RequestMapping(value = "/news/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新资讯")
    public Result<Object> updateNews(TbNews tbNews){
        tbNews.setUpdateBy(getCurrentUser().getId());
        newsService.updateNews(tbNews);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/news/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增资讯")
    public Result<Object> addStock(TbNews tbNews){
        TbUser user = getCurrentUser();
        tbNews.setCreateBy(user.getId());
        tbNews.setUpdateBy(user.getId());
        newsService.addNews(tbNews);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/news/{id}",method = RequestMethod.GET)
    public Result<TbNews> getNews(@PathVariable Long id){
        TbNews tbNews = newsService.getNewsById(id);
        return new ResultUtil<TbNews>().setData(tbNews);
    }


    @RequestMapping(value = "/news/alertStatus",method = RequestMethod.POST)
    @ApiOperation(value = "发布或者取消发布")
    public Result<Object> alertNewsStatus(Long id,Integer status){
        newsService.alterNewsStatus(id,status);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/news/top",method = RequestMethod.POST)
    @ApiOperation(value = "置顶")
    public Result<Object> topNews(Long id){
        newsService.topNews(id);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/news/cancelTop",method = RequestMethod.POST)
    @ApiOperation(value = "取消置顶")
    public Result<Object> cancelTopNews(Long id){
        newsService.cancelTopNews(id);
        return new ResultUtil<Object>().setData(null);
    }

}
